﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DA
{
    public class SanPhamDA:BaseConnection
    {
        public SqlDataAdapter danhSachSanPhamAdapter {set;get;}
        public SqlDataAdapter timKiemSanPhamAdapter { set; get; }

        public SanPhamDA()
        {
           
        }

        public DataSet layDanhSachSanPham()
        {
            String sqlStm = "SELECT MaSanPham FROM ThongTinSanPham";
            return layDuLieuTheoCauLenhSQL(sqlStm);
        }

        public DataSet timKiemSanPham(string maSanPham)
        {
            DataSet ds = new DataSet();
            String sqlStatement = "";
                sqlStatement = "SELECT MaSanPham, TenSanPham, KichThuoc, HinhVe, SoMau, MaRap, MaKhuon, MaNapChup, DoKho";
		        sqlStatement += " FROM ThongTinSanPham";
                sqlStatement += " WHERE MaSanPham = '"+ maSanPham +"'";

            openConnect();
         
            SqlCommand cmd = new SqlCommand(sqlStatement, con);
            timKiemSanPhamAdapter = new SqlDataAdapter(cmd);
            SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(timKiemSanPhamAdapter);
            timKiemSanPhamAdapter.Fill(ds, "SanPham");
            closeConnect();
            return ds;         
        }

        public DataSet timKiemSanPham(int maSanPham)
        {
            throw new NotImplementedException();
        }

        public DataSet thongTinSanPham( string maSanPham)
        {
            DataSet ds = new DataSet();
            String sqlStatement = " select KichThuoc , SoMau FROM ThongTinSanPham WHERE MaSanPham = '"+ maSanPham +"'";

            openConnect();

            SqlCommand cmd = new SqlCommand(sqlStatement, con);
            danhSachSanPhamAdapter = new SqlDataAdapter(cmd);
            danhSachSanPhamAdapter.SelectCommand = cmd;
            SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(danhSachSanPhamAdapter);
            danhSachSanPhamAdapter.Fill(ds, "SanPham");
            closeConnect();
            return ds;       

        }
        public int taoSanPhamMoi (string maSanPham, string tenSanPham, string soMau, string kichThuoc, string doKho, string maRap, string maKhuon, string maNapChup, string hinhVe)
        {
            String sqlStm = "INSERT INTO ThongTinSanPham(MaSanPham, TenSanPham, SoMau, KichThuoc, DoKHo, MaRap, MaKhuon, MaNapChup, HinhVe) VALUES(@maSanPham, @tenSanPham, @soMau, @kichThuoc, @doKho, @maRap, @maKhuon, @maNapChup, @hinhVe)";
            openConnect();

            //VALUES(@maSanPham, @tenSanPham, @soMau, @kichThuoc, @doKho, @maRap, @maKhuon, @maNapChup)"
            SqlCommand cmd = new SqlCommand(sqlStm, con);
            cmd.Parameters.Add("@maSanPham", SqlDbType.VarChar).Value = maSanPham;
            cmd.Parameters.Add("@tenSanPham", SqlDbType.VarChar).Value = tenSanPham;
            cmd.Parameters.Add("@soMau", SqlDbType.Int).Value = int.Parse(soMau);
            cmd.Parameters.Add("@kichThuoc", SqlDbType.VarChar).Value = kichThuoc;
            cmd.Parameters.Add("@doKho", SqlDbType.Int).Value = int.Parse(doKho);
            cmd.Parameters.Add("@maRap", SqlDbType.VarChar).Value = maRap;
            cmd.Parameters.Add("@maKhuon", SqlDbType.VarChar).Value = maKhuon;
            cmd.Parameters.Add("@maNapChup", SqlDbType.VarChar).Value = maNapChup;
            cmd.Parameters.Add("@hinhVe", SqlDbType.VarChar).Value = hinhVe;
            return cmd.ExecuteNonQuery();
            
        }
    }
}
